<template>
    <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
// require('echarts/theme/macarons') // echarts theme
export default {

  props: {
    PieData: Array,
    required: true,
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '330px'
    }
  },
  data() {
    return {
      chart: null,
      cData:[]
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c} ({d}%)',
          background:'red'
        },
        legend: {
          left: 'center',
          bottom: '10'
        //   data: ['Industries', 'Technology', 'Forex', 'Gold', 'Forecasts']
        },
        series: [
          {

            type: 'pie',
            // roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '50%'],
            data: this.cData,
            animationEasing: 'cubicInOut',
            animationDuration: 2600,

            // emphasis: {
            //   itemStyle: {
            //     shadowBlur: 10,
            //     shadowOffsetX: 0,
            //     shadowColor: 'rgba(0, 0, 0, 0.5)'
            //   }
            // }
          }
        ]
      })
    }
  },
  watch:{
    PieData(newVal,oldVal){
      this.cData=JSON.parse(JSON.stringify(newVal).replace(/count/g, 'value').replace(/username/g, 'name'))
      console.log(this.cData,55555)
      this.initChart()
    }
  }
}
</script>
